#!/usr/bin/env bash

# 生成ssh public key
# 参数: 运行的命令
# 例: ssh_keygen 'ssh-keygen'
# 例: ssh_keygen 'ssh root@db1 ssh-keygen'
function ssh_keygen(){
    expect -c "
        spawn $1
        expect \"id_rsa): \"
        send \"\r\"
        expect {
            \"(empty for no passphrase): \" {send \"\r\";}
            \"Overwrite (y/n)? \" {send \"n\r\";}
        }
        expect {
            eof {exit;}
            \"again: \" {send \"\r\";}
        }
        expect eof
    "
}

# 设置无密码连接
# 参数1: 运行的命令
# 参数2: 密码
# 例: ssh_nopasswd "ssh-copy-id root@db1" 123456
# 例: ssh_nopasswd "ssh root@db1 ssh-copy-id root@db2" 123456
function ssh_nopasswd()
{
    expect -c "
        spawn $1
        expect {
            eof {exit;}
            \"continue connecting (yes/no)? \" {send \"yes\r\";}
            \"password: \" {send \"$2\r\";}
        }
        expect {
            eof {exit;}
            \"password: \" {send \"$2\r\";}
        }
        expect eof
    "
}

# 打印帮助信息
function echo_help()
{
    echo "######################################################"
    echo -e "$1"
    echo "######################################################"
}

# 远程root用户执行命令
# 第一个参数为host
# 第二个为命令
# 例：ssh_root_exec db1 "ls"
function ssh_root_exec()
{
    ssh root@$1 $2
}

# 获取配置
# 没有参数时获取所有配置
# 有两个参数时，根据参数获取配置
function get_config()
{
    if [ $# -eq 0 ]; then
        args=`python get_config.py`
        for arg in ${args[@]}
        do
            eval $arg=`python get_config.py default $arg`
        done
    elif [ $# -eq 2 ]; then
        echo `python get_config.py $1 $2`
    else
        echo ''
    fi
}

# 修改/etc/hosts
function update_etc_hosts()
{
    hosts=$@
    for host in ${hosts[@]}
    do
        ip=`get_config $host ip`
        passwd=`get_config $host root_password`
        ssh_nopasswd "ssh-copy-id root@$ip" $passwd
        for _host in ${hosts[@]}
        do
            _ip=`get_config $_host ip`
            ssh_root_exec $ip "sed -i '/$_ip/d' /etc/hosts"
            ssh_root_exec $ip "sed -i '1i\\$_ip $_host' /etc/hosts"
        done
    done
}


# 设置无密码连接
# 参数: hosts
function set_no_passwd()
{
    ssh_keygen "ssh-keygen"
    hosts=$@
    for host in ${hosts[@]}
    do
        user=`get_config $host user`
        passwd=`get_config $host "$user"_password`
        ssh_nopasswd "ssh-copy-id $user@$host" $passwd
        ssh_keygen "ssh $user@$host ssh-keygen"
        for _host in ${hosts[@]}
        do
            _user=`get_config $_host user`
            _passwd=`get_config $host "$_user"_password`
            ssh_nopasswd "ssh -t $user@$host ssh-copy-id $_user@$_host" $_passwd
        done
    done
}

function check_args()
{
    args=$@
    len=$#
    if [ $len -eq 1 ]; then
        arg=$1
        case $arg in
            '--help')        nopsd_help;;
            '-h')            nopsd_help;;
            '-n')            nopsd "no-update";;
            '--no-update')   nopsd "no-update";;
            *)               nopsd_help;;
        esac
    elif [ $len -eq 0 ]; then
        nopsd "update"
    else
        nopsd_help
    fi
}

function nopsd_help()
{
    echo "用法: nopsd [-h|--help|--no-update]"
    echo "    -h, --help          打印帮助信息"
    echo "    -n, --no-update      关闭修改/etc/hosts,默认开启"
}

function nopsd()
{
    if [ "$1" = "update" ]; then
        echo_help "更新/etc/hosts"
        update_etc_hosts $hosts
    fi
    echo_help "设置无密码连接"
    set_no_passwd $hosts
}
